package com.yyq.javastudy;

import java.util.Objects;

/**
 * @author yyq
 * @create 2021-08-01 15:45
 */
public class Persion implements Comparable{
    public int age;
    String name;
    static  int total;
    static {
        total=1000;
    }

    public Persion(int age,String name){
        this.age=age;
        this.name=name;
        total++;
    }

    public void myself(){
        System.out.println("Age="+age+"Name="+name);
    }

    @Override
    public boolean equals(Object o) {
        System.out.println("调用了Persion Equals方法啦~~~~~");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Persion persion = (Persion) o;
        return age == persion.age &&
                Objects.equals(name, persion.name);
    }

    @Override
    public int hashCode() {
        System.out.println("调用Persion-hashcode方法啦啦啦啦啦");
        return Objects.hash(age, name);
    }

    @Override
    public String toString() {
        return "Persion{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Persion){
            Persion p=(Persion)o;
            int i = this.name.compareTo(p.name);
            if(i!=0)
                return i;
            else
            return -Integer.compare(this.age,p.age);

        }
        throw new RuntimeException("数据类型错误");
    }
}
